package operation;

import student.Student;
import student.StudentArray;

import java.util.Scanner;

public class DelOperation implements IOperation{
    @Override
    public void work(StudentArray stulist) {
        System.out.println("删除学生信息");
        int sz = stulist.getUsedSize();
        if(sz == 0){
            try {
                throw new DelException("学生表为空，不能删除");
            } catch (DelException e) {
                throw new RuntimeException(e);
            }
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除学生的学生号");
        int id = sc.nextInt();
        int count = 0;
        int i = 0;
        for(; i < sz; i++){
            Student stu = stulist.getStudent(i);
            if(stu.getStudentId() == id){
                count = i;
                break;
            }
        }
        if(i == sz){
            System.out.println("当前学生表没有找到该学生");
            return;
        }
        for(i = count; i < sz - 1; i++){
            Student stu1 = stulist.getStudent(i+1);
            stulist.setStudent(i,stu1);
        }
        stulist.setStudent(i,null);
        stulist.setUsedSize(sz-1);
        System.out.println("删除成功");
    }
}
